package org.extremengine.common;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public class Utils {
	
	/**
	 * Whether the number is plus or minus, 0 is plus.
	 * @param number
	 * @return
	 */
	public static double isPlusOrMinus(double number) {
		return (number >= 0.0) ? 1.0 : -1.0;
	}
	
	public static int isPlusOrMinus(int number) {
		return (number >= 0.0) ? 1 : -1;
	}

	public static HashSet cloneSet(Set src) {
		HashSet clone = new HashSet();
		for (Iterator it = src.iterator(); it.hasNext();) {
			Object object = (Object) it.next();
			clone.add(object);
		}
		return clone;
	}

	/**
	 * Default keep after decimal point is 2.
	 * @param d
	 * @return
	 */
	public static String trimAfterDecimalPoint(double d) {
		return trimAfterDecimalPoint(d, 2);
	}

	/**
	 * Trim numbers after decimal point.
	 * @param d
	 * @param keep How many to keep after point.
	 * @return
	 */
	public static String trimAfterDecimalPoint(double d, int keep) {
		String str = String.valueOf(d);
		int pidx = str.indexOf('.');
		if (pidx > 0 && pidx + keep < str.length()) {
			str = str.substring(0, pidx + 1 + keep);
		}
		return str;
	}
}
